<template>
    <div class="treeBox">
        <div class="title" v-show="!hideTitle">
            请选择{{placeholder}}
            <van-icon name="cross" @click="close" />
        </div>
        <div class="main">
            <SearchTree ref="tree" node-key="value" :data="treeList" name='tree' :show-checkbox="false" :default-expand-all="false" :expand-on-click-node="false" :check-on-click-node="true" :default-expanded-keys="defaultExpanded" @node-click="handleClick">
                <template slot-scope="node">
                    <div style="margin: 5px" class="flex">
                        <div>{{node.title}}</div>
                        <p class="successIcon">
                            <van-icon name="success" v-show="node.value == currentValue" />
                        </p>
                    </div>
                </template>
            </SearchTree>
            <p v-show="!treeList.length">暂无数据</p>
        </div>
    </div>
</template>

<script>
    export default {
        name: "treeSelect",
        props: {
            // 层级数据
            treeList: {
                type: Array,
                default: () => []
            },
            // 标题
            placeholder: {
                type: String,
                default: ''
            },
            // 当前选中的value值
            curValue: {
                type: String,
                default: ''
            },
            // 是否隐藏标题
            hideTitle: {
                type: Boolean,
                default: () => {
                    return false;
                }
            },
            // 是否只能选择子节点
            onlyChild: {
                type: Boolean,
                default: () => {
                    return false;
                }
            }
        },
        data () {
            return {
                defaultExpanded: []
            }
        },
        computed: {
            currentValue: {
                get () {
                    return this.curValue
                },
                set () { }
            }
        },
        methods: {
            // 点击节点
            handleClick (e, node) {
                if (this.onlyChild) {
                    if (node.children && node.children.length) { } else {
                        this.currentValue = node.value;
                        this.$emit('on-change', node);
                        this.close();
                    }
                } else {
                    this.currentValue = node.value;
                    this.$emit('on-change', node);
                    this.close();
                }
            },
            close () {
                this.currentValue = '';
                if (!this.hideTitle) {
                    this.$emit('close');
                }
            }
        }
    }
</script>

<style scoped lang='less'>
    .treeBox {
        width: 100%;
        height: 100%;
        .title {
            height: 40px;
            line-height: 40px;
            width: 100%;
            text-align: center;
            border-bottom: 1px solid #eee;
            font-size: 14px;
            position: relative;
            .van-icon {
                position: absolute;
                right: 10px;
                line-height: 40px;
                font-weight: normal;
                font-size: 17px;
            }
        }
        .main {
            width: 100%;
            height: calc(100% - 40px);
            overflow: auto;
            /* 层级树 */
            /deep/.ldq-tree {
                line-height: 25px;
                font-size: 14px;
                .gary {
                    color: #333 !important;
                }
                .tree-triangle {
                    border-top: 7px solid #888 !important;
                    border-right: 5px solid transparent !important;
                    border-left: 5px solid transparent !important;
                }
                .flex {
                    display: flex;
                    justify-content: space-between;
                    & > div {
                        flex: 1;
                        overflow: hidden;
                        text-overflow: ellipsis;
                        white-space: nowrap;
                    }
                    .successIcon {
                        float: right;
                        margin-left: 15px !important;
                        margin-right: 20px !important;
                        font-size: 18px;
                        font-weight: 900;
                        color: rgb(20, 160, 216);
                    }
                }
            }
            & > p {
                text-indent: 2em;
                margin-top: 20px;
            }
        }
    }
</style>